<?php

/**
 *权限控制器
 *控制器->方法
*/

namespace app\admin\controller;

use app\admin\model\Permission as PerModel;
use think\Request;

class Permission extends Base
{
    protected $permission_model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $permission_model = new PerModel();
        $this->permission_model = $permission_model;
    }

    public function index()
    {
        return $this->listing();
    }

//权限列表
    public function listing()
    {
        $page_counts = 20;
        $counts = $this->permission_model->count('id');
        $data = $this->permission_model->field(true)->paginate($page_counts, $counts);
        $pages = intval(ceil($counts/$page_counts));

        $this->assign("pages", $pages);
        $this->assign('counts', $counts);
        $this->assign("data", $data);
        return $this->fetch('listing');
    }

//添加分组
    public function add()
    {
        if (Request::instance()->isPOST()) {
            // var_dump(input('post.'));exit;
                
            if (input('post.permi_method')=='' ||  input('post.permi_name')=='') {
                $this->error("权限不能为空");
            }
            $this->permission_model->pid = intval(trim(input('post.pid')));
            $this->permission_model->permi_method = trim(input('post.permi_method'));
            $this->permission_model->permi_name = trim(input('post.permi_name'));
            $this->permission_model->description = trim(input('post.note'));

            try {
                $this->permission_model->save();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);
                // echo $msg;exit;
                $this->error("错误提示:".$msg);
            }
            $this->success('添加成功', 'index');
        } else {
            $data =  $this->permission_model->permitCrtl();

            $this->assign('data', $data);
            return $this->fetch();
        }
    }

//编辑分组
    public function edit($id = "")
    {
        $data = $this->permission_model->find($id);

        if (Request::instance()->isPOST()) {
            // var_dump(input('post.'));
            // var_dump($id);exit;
            if (input('post.permi_name')=='' || input('post.permi_method')=='') {
                $this->error("组名不能为空");
            }
            $data->pid = intval(trim(input('post.pid')));
            $data->permi_method = trim(input('post.permi_method'));
            $data->permi_name = trim(input('post.permi_name'));
            $data->description = trim(input('post.note'));
          
            try {
                $data->save();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);

                $this->error("错误提示:".$msg);
            }
            $this->success('编辑成功', 'index');
        } else {
            // var_dump($data);exit;
            $data_ctrl =  $this->permission_model->permitCrtl();
            $this->assign('data_ctrl', $data_ctrl);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            if (is_array($id)) {
                foreach ($id as $v) {
                    $data = model('permission')->where('pid', $v)->find();
                    if ($data) {
                        return  ['status'=>0,'msg'=>'不可删除,控制器下的方法不为空'];
                    }
                }
            } else {
                $data = model('permission')->where('pid', $id)->find();
                if ($data) {
                    return  ['status'=>0,'msg'=>'不可删除,该控制器下的方法不为空'];
                }
            }
            if ($this->permission_model->destroy($id)) {
                return ['status'=>1,'msg'=>'删除成功'];
            } else {
                return ['status'=>0,'msg'=>'删除失败'];
            }
        }
    }
}
